//
//  ClassifyViewController.m
//  Project_A
//
//  Created by 凯 on 15/7/13.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "ClassifyViewController.h"
#import "BaseHeader.h"
#import "ClassifyLiftCollectionViewCell.h"
#import "ClassifyRightCollectionViewCell.h"
#import "SecondViewController.h"
#import "AFNetWorkHandle.h"
#import "XiaoKaiBrand_detailsViewController.h"
#import "XiaoKaiSearchViewController.h"
@interface ClassifyViewController ()
<
UICollectionViewDataSource,
UICollectionViewDelegate
>

@property (nonatomic , retain)NSMutableArray *classifymodelarr;
@property (nonatomic, retain)UIButton *back;
@end

@implementation ClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //collectionview layout
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置状态栏颜色为亮色
    [UIApplication sharedApplication].statusBarStyle  = UIStatusBarStyleLightContent;
    //创建假的导航栏
    
    //隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
//    //contentview 大小
//    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 2.6 );
//    //上  左  下 右 间距
//    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50);
//    layout.minimumLineSpacing = -10;
//    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
//    collectionView.delegate = self;
//    collectionView.dataSource = self;
//    //    [collectionView insertRowsAtIndexPaths:indexPathArray withRowAnimation:uicollectionv];
//    [collectionView registerClass:[ClassifyLiftCollectionViewCell class] forCellWithReuseIdentifier:@"lift"];
//    [collectionView registerClass:[ClassifyRightCollectionViewCell class] forCellWithReuseIdentifier:@"right"];
//    collectionView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:collectionView];
//    [layout release];
    [self getData];
    
    
    //    //注册头部区域
    //    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
-(void)getData
{
    NSString *str = @"http://api.liwushuo.com/v2/brands/editor?limit=20&offset=0";
    self.classifymodelarr = [NSMutableArray array];
    [AFNetWorkHandle GETwithAFNByURL:str completion:^(id result) {
        NSMutableArray *temparr = [[result objectForKey:@"data"]objectForKey:@"brands"];
      //  NSLog(@"%@",temparr);
        for (NSDictionary *dic in temparr) {
            ClassifyModel *model = [[ClassifyModel alloc]initWithDic:dic];
            [self.classifymodelarr addObject:model];
            //NSLog(@"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~get~~~~~~~~~~~~~%@",self.classifymodelarr);
          
        }
         [self creatCollectionView];
    }];

}
-(void)creatCollectionView
{
       UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //contentview 大小
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 2.6 );
    //上  左  下 右 间距
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 69);
    //间隙 
  //  layout.minimumLineSpacing = -10;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    //   [collectionView insertRowsAtIndexPaths:indexPathArray withRowAnimation:uicollectionv];
    [collectionView registerClass:[ClassifyLiftCollectionViewCell class] forCellWithReuseIdentifier:@"lift"];
    [collectionView registerClass:[ClassifyRightCollectionViewCell class] forCellWithReuseIdentifier:@"right"];
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    [self creatNav];
    [self getbutton];
}
-(void)creatNav
{
    UIView *naview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20 )];
    naview.backgroundColor = NAVCOLOR;
    [self.view addSubview:naview];
    UILabel *Navlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    Navlabel.backgroundColor = NAVCOLOR;
    Navlabel.text = @"品牌名店";
    Navlabel.font = [UIFont systemFontOfSize:22];
    Navlabel.textColor = [UIColor whiteColor];
    Navlabel.textAlignment = NSTextAlignmentCenter;
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(SCREEN_WIDTH - 50, 25, 32, 32);
//    button.backgroundColor = [UIColor orangeColor];
//    [button setImage:[UIImage imageNamed:@"iconfont-like"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(sousuo) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:Navlabel];
    [self.view bringSubviewToFront:Navlabel];
//    [self.view addSubview:button];
//    [self.view bringSubviewToFront:button];
}
//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    //通过判断kind参数类型 确定是头部区域还是尾部区域
//    if (kind == UICollectionElementKindSectionHeader) {
//        //从头部区域 重用池中获取f头部对象
//        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//        header.backgroundColor = [UIColor greenColor];
//
//
//        return header;
//    }
//    return nil;
//}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.classifymodelarr.count;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item % 2 == 0) {
        ClassifyLiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lift" forIndexPath:indexPath];
        ClassifyModel *model = [self.classifymodelarr objectAtIndex:indexPath.row];
        cell.Model = model;
        return cell;
    }else {
        ClassifyRightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"right" forIndexPath:indexPath];
        ClassifyModel *model = [self.classifymodelarr objectAtIndex:indexPath.row];
        cell.Model = model;
        return cell;
    }
    
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XiaoKaiBrand_detailsViewController *brand = [[XiaoKaiBrand_detailsViewController alloc]init];
    //   sec.hidesBottomBarWhenPushed = YES;
    ClassifyModel *model = [self.classifymodelarr objectAtIndex:indexPath.row];
    brand.str3 = model.idstr;
    brand.headbackimage = model.cover_image_url;
    brand.headtitle = model.name;
    [self.navigationController pushViewController:brand animated:YES];

}
-(void)getbutton
{
    self.back = [UIButton buttonWithType:UIButtonTypeCustom];
    self.back.backgroundColor = [UIColor clearColor];
    self.back.frame = CGRectMake(10, 32, 20, 20);
    [self.back setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangzuo@2x"] forState:UIControlStateNormal];
    [self.back addTarget:self action:@selector(backaaa) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.back];
    [self.view bringSubviewToFront:self.back];
}
-(void)backaaa
{
    [self.navigationController popViewControllerAnimated:YES];
    //    NSLog(@"走走走 回家喽");
    
}
//-(void)sousuo
//{
//    XiaoKaiSearchViewController *seac = [[XiaoKaiSearchViewController alloc]init];
//    [self.navigationController pushViewController:seac animated:YES];
//}

//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
